﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace WCFPanificadora.serial
{
    public static class GeradorSerial
    {
        // Similar sets have been omitted, like 1,I; Q,O,0;
        private static readonly char[] alphabet = "ABCDEFGHJKLMNPRSTUVWXYZ23456789".ToCharArray();
        private static readonly Random rand = new Random();
        internal static string GetNext(int codeLength, char separatorChar, int segmentLength)
        {
            char[] randChars = randomAlphabetChars(codeLength);
            char[] formattedChars = new char[codeLength + (codeLength / segmentLength) - 1];
            int numberOfSeparators = 0;
            for (int i = 0; i < randChars.Length; i++)
            {
                if (i % segmentLength == 0 && i != 0)
                    formattedChars[i + numberOfSeparators++] = separatorChar;
                formattedChars[i + numberOfSeparators] = randChars[i];
            }
            return new string(formattedChars);
        }
        private static char[] randomAlphabetChars(int length)
        {
            char[] newChars = new char[length];
            for (int i = 0; i < length; i++)
                newChars[i] = alphabet[(int)Math.Truncate(rand.NextDouble() * 1000) % alphabet.Length];
            return newChars;
        }


    }
}